package com.shiftychess.view.components{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import mx.controls.Image;
	import mx.core.UIComponent;

	public class ChessPieceDesktopIcon extends UIComponent{

        [Embed(source='WhitePawn.gif')]
        public var WhitePawn:Class;
        
        [Embed(source='WhiteRook.gif')]
        public var WhiteRook:Class;
        
        [Embed(source='WhiteBishop.gif')]
        public var WhiteBishop:Class;
        
        [Embed(source='WhiteQueen.gif')]
        public var WhiteQueen:Class;
        
        [Embed(source='WhiteKing.gif')]
        public var WhiteKing:Class;
        
        [Embed(source='BlackPawn.gif')]
        public var BlackPawn:Class;
        
        [Embed(source='BlackRook.gif')]
        public var BlackRook:Class;
        
        [Embed(source='BlackBishop.gif')]
        public var BlackBishop:Class;
        
        [Embed(source='BlackQueen.gif')]
        public var BlackQueen:Class;
        
        [Embed(source='BlackKing.gif')]
        public var BlackKing:Class;
		
		public static var directory_bmd:BitmapData;
		public static var textfile_bmd:BitmapData;
		
		public static function setIcons(dir:BitmapData,txtfile:BitmapData):void{
			directory_bmd=dir;
			textfile_bmd=txtfile;
		}
		
		protected var _icon:Bitmap;
		protected var _chess_piece:Bitmap;
		protected var _id:int;
		protected var _name:String;
		protected var _label:TextField;
		
		public function getId():int{return _id;}
		
		public function ChessPieceDesktopIcon(color:String,name:String="hello"){
			super();
			_name=name;
			render(color);
		}
		
		//template method, must be implemented by subclasses
		protected function render(color:String):void{
			//throw error
			if(textfile_bmd==null || directory_bmd==null){
				//throw a nasty error, return
			}
			if(color.toLowerCase()=="white"){
				_icon=new Bitmap(directory_bmd);
				_chess_piece=new WhitePawn();
			}else{
				_icon=new Bitmap(textfile_bmd);
				_chess_piece=new BlackPawn();
			}
			addChild(_icon);
			addChild(_chess_piece);
			_chess_piece.x=_icon.width/2-_chess_piece.width/2;
			_chess_piece.visible=false;
			_label=new TextField();
			_label.text=_name;
			_label.selectable=false;
			_label.autoSize=TextFieldAutoSize.LEFT;
			var tf:TextFormat=new TextFormat("Verdana",10,0xFFFFFF);
			_label.setTextFormat(tf);
			_label.x=Math.round(_icon.bitmapData.width/2-_label.width/2);
			_label.y=_icon.height;
			addChild(_label);
			
		}

		public function getDragImage():Image{		
			var bmd:BitmapData=new BitmapData(_icon.width,_icon.height,true,0x00);
			bmd.draw(_icon);
			var imageProxy:Image = new Image();
			imageProxy.addChild(new Bitmap(bmd));
			imageProxy.width=bmd.width;
			imageProxy.height=bmd.height;
			return imageProxy;
		}
		public function get icon_width():uint{
			return _icon.width;
		}
		
		public function get icon_height():uint{
			return _icon.height;
		}
	
		public function showChessPiece():void{
			_icon.visible=false;
			_chess_piece.visible=true;
		}
		public function hideChessPiece():void{
			_icon.visible=true;
			_chess_piece.visible=false;
		}
		
	}
}